#include <assert.h>
#include <stdlib.h>
#include "anim.h"

void anim_init (Anim* anim, int count, int duration, bool loops)
{
	assert(anim != NULL);
	assert(count > 0);
	assert(((count == 1) && (duration >= 0)) || (duration > 0));
	
	anim->frame = 0;
	anim->count = count;
	anim->duration = duration;
	anim->cooldown = duration;
	anim->loops = loops;
}

void anim_update (Anim* anim)
{
	if ((!anim->loops) && (anim->frame == anim->count-1)) {
		return;
	}
	
	anim->cooldown--;
	if (anim->cooldown <= 0) {
		anim->cooldown = anim->duration;
		anim->frame++;
		if (anim->frame == anim->count) {
			anim->frame = 0;
		}
	}
}
